#pragma once
#include"mysql.hpp"
#include "chat_session_member.hxx"
#include "chat_session_member-odb.hxx"
namespace wzl
{
    class ChatSessionMemberTable
    {
    public:
        using ptr = std::shared_ptr<ChatSessionMemberTable>;
        ChatSessionMemberTable(const std::shared_ptr<odb::core::database>& db)
            :_db(db)
        {}
        bool append(ChatSessionMember& csm)    //单个会话成员新增  ssid & uid
        {
            try {
                odb::transaction trans(_db->begin());
                _db->persist(csm);
                trans.commit();
            }catch (std::exception &e) {
                LOG_ERROR("新增单会话成员失败 {}:{} !", csm.user_id(),e.what());
                return false;
            }
            return true;
        }

        bool append(std::vector<ChatSessionMember>& csm_lists)    //多个会话成员新增
        {
            try {
                odb::transaction trans(_db->begin());
                for(auto& csm :csm_lists)
                {
                    _db->persist(csm);
                }
                trans.commit();
            }catch (std::exception &e) {
                LOG_ERROR("新增多会话成员失败 {}-{}:{} !",csm_lists.size(),csm_lists[0].session_id(),e.what());
                return false;
            }
            return true;
        }

        bool remove(ChatSessionMember& csm)    //删除指定会话中指定成员  ssid & uid
        {
            try {
                odb::transaction trans(_db->begin());
                typedef odb::query<ChatSessionMember> query;
                typedef odb::result<ChatSessionMember> result;
                _db->erase_query<ChatSessionMember>(query::session_id == csm.session_id() && query::user_id == csm.user_id());
                trans.commit();
            }catch (std::exception &e) {
                LOG_ERROR("删除单会话成员失败 {}:{} !",csm.session_id(),e.what());
                return false;
            }
            return true;
        }

        bool remove(const std::string& ssid)    //删除指定会话中的所有成员
        {
            try {
                odb::transaction trans(_db->begin());
                typedef odb::query<ChatSessionMember> query;
                typedef odb::result<ChatSessionMember> result;
                _db->erase_query<ChatSessionMember>(query::session_id == ssid);
                trans.commit();
            }catch (std::exception &e) {
                LOG_ERROR("删除多会话成员失败 {} !",ssid,e.what());
                return false;
            }
            return true;
        }

        std::vector<std::string> members(const std::string& ssid)  //获取会话中的所有成员
        {
            std::vector<std::string> res;
            try {
                odb::transaction trans(_db->begin());
                typedef odb::query<ChatSessionMember> query;
                typedef odb::result<ChatSessionMember> result;
                result r(_db->query<ChatSessionMember>(query::session_id == ssid));
                for (result::iterator i(r.begin()); i != r.end(); ++i) {
                    res.push_back(i->user_id());
                }
                trans.commit();
            }catch (std::exception &e) {
                LOG_ERROR("通过用户ID批量查询用户失败:{}-{}!",ssid,e.what());
            }
            return res;
        }
    private:
        std::shared_ptr<odb::core::database> _db;
    };
}